package nc.bs.pub.action;

import java.util.*;

import nc.bs.dao.BaseDAO;
import nc.bs.framework.common.InvocationInfoProxy;
import nc.bs.framework.common.NCLocator;
import nc.bs.pub.compiler.IWorkFlowRet;
import nc.jdbc.framework.processor.ColumnProcessor;
import nc.pubitf.initgroup.InitGroupQuery;
import nc.vo.arap.bx.util.BXUtil;
import nc.vo.hrpom.prosetuptask.AggProSetupTaskVO;
import nc.vo.hrpom.prosetuptask.JoinUnitVO;
import nc.vo.hrpom.prosetuptask.ProSetupTaskVO;
import nc.vo.hrpom.prosetuptask.TopicOutlayVO;
import nc.vo.hrppl.pjapplication.*;
import nc.vo.hrppl.proupdate.*;
import nc.vo.org.DeptVO;
import nc.vo.pub.BusinessException;
import nc.vo.pub.compiler.PfParameterVO;
import nc.vo.pub.lang.UFBoolean;
import nc.vo.pub.lang.UFDate;
import nc.vo.pub.lang.UFDouble;
import nc.vo.pub.VOStatus;
import nc.vo.uap.pf.PFBusinessException;
import nc.vo.pub.ISuperVO;
import nccloud.commons.lang.ArrayUtils;
import nccloud.commons.lang.StringUtils;
import nc.codeplatform.framework.service.AbstractPfScriptAction;
import nc.impl.pubapp.pattern.data.bill.BillQuery;
import nc.impl.pubapp.pattern.data.bill.BillUpdate;
import nc.vo.pmpub.project.ProjectBillVO;
import nc.vo.pmpub.project.ProjectHeadVO;
import nc.itf.hrppl.pjapplication.pjappl.IPJApplHVOService;
import nc.itf.hrppl.proupdate.proupdatehvo.IProUpdateHVOService;
import nc.itf.pmpub.prv.IProject;
import nccloud.itf.hrpom.project.IProsetuptaskvoMaintain;

public class N_PL02_APPROVE extends AbstractPfScriptAction<AggProUpdateHVO> {

    private static BaseDAO baseDAO;

    private static BaseDAO getBaseDAO() {
        if (baseDAO == null) {
            baseDAO = new BaseDAO();
        }

        return baseDAO;
    }

    public N_PL02_APPROVE() {
        super();
    }

    // 是否安装了科研 并启用
    private static boolean isInstalledAndEnabledHrpom() throws BusinessException {
        boolean flag = (BXUtil.isProductTbbInstalled("M072")
                && InitGroupQuery.isEnabled(InvocationInfoProxy.getInstance().getGroupId(), "M072"));
        return flag;
    }

    @SuppressWarnings("unused")
    @Override
    protected Object process(PfParameterVO paraVO) throws BusinessException {
        try {
            IProUpdateHVOService service = NCLocator.getInstance().lookup(IProUpdateHVOService.class);
            AggProUpdateHVO[] vos = (AggProUpdateHVO[]) this.getVos();
            Arrays.stream(vos).forEach(v -> v.getParent().setStatus(VOStatus.UPDATED));

            IWorkFlowRet workFlowRet = (IWorkFlowRet) this.procActionFlow(paraVO);

            vos = service.callbackAPPROVE(vos);
            // 审批通过之后修改对应的申请单 & 项目-集团/项目-组织 对应的项目档案 & 科研/专项 立项任务书
            // 要判断审批状态 存在多级审批的情况
            if (vos[0].getParentVO().getApprovestatus() == 1) {

                ProUpdateHVO hvo = vos[0].getParentVO();
                String pk_proapp = hvo.getPk_proapp();
                // 获取项目申请的信息
                BillQuery<AggPjapplVO> billQuery = new BillQuery<AggPjapplVO>(AggPjapplVO.class);

                // 获取对应的项目档案
//                ProjectHeadVO[] projectHeadVOs = NCLocator.getInstance().lookup(nc.itf.pmpub.prv.IProjectQuery.class)
//                        .queryProjectHeadVOsBypks(new String[]{vos[0].getParentVO().getPk_proapp()});

                ProjectBillVO[] projectBillVOs = NCLocator.getInstance().lookup(nc.itf.pmpub.prv.IProjectQuery.class)
                        .queryProjectBillVOsBypks(new String[]{vos[0].getParentVO().getPk_proapp()});
                if(ArrayUtils.isEmpty(projectBillVOs)){
                    throw new BusinessException("项目调整单对应的项目申请单未进行项目准入,请联系对应人员进行项目准入!");
                }

                ProjectHeadVO projectHeadVOs = projectBillVOs[0].getParentVO();

                projectHeadVOs.setStatus(1); //修改状态

                //***************************************处理:申请单&项目*************************************************
                AggPjapplVO[] pjapplVOs = billQuery.query(new String[]{vos[0].getParentVO().getPk_proapp()});
                if (pjapplVOs == null || pjapplVOs.length == 0) {
                    throw new BusinessException("项目调整单对应的项目申请单不存在,请联系对应人员!");
                }

                // 项目调整主表
                ProUpdateHVO update_parentVO = vos[0].getParentVO();
                // 项目申请主表
                PJApplHVO pjappl_parentVO = pjapplVOs[0].getParentVO();

                // 1.项目名称修改 旧名称记录为曾用名
                if (!update_parentVO.getProname().equals(pjappl_parentVO.getPjname())) {
                    pjappl_parentVO.setPjusename(pjappl_parentVO.getPjname()); // 曾用名
                    pjappl_parentVO.setPjname(vos[0].getParentVO().getProname()); // 新名称

                    projectHeadVOs.setProject_ot_name(pjappl_parentVO.getPjname()); // 曾用名
                    projectHeadVOs.setProject_name(vos[0].getParentVO().getProname());// 新名称
                }
                // 2.父项目 不必填
                pjappl_parentVO.setBelongpro(update_parentVO.getBelongpro());

                // 3.调整后总经费->项目总经费
                pjappl_parentVO.setPjtotalcost(update_parentVO.getUpdallfun());
                // 4.主管科室
                if (!update_parentVO.getChargedept().equals(pjappl_parentVO.getOfficedept())) {
                    pjappl_parentVO.setOfficedept(update_parentVO.getChargedept());


                    projectHeadVOs.setPk_duty_dept(update_parentVO.getChargedept());
                    Collection<DeptVO> dept = new BaseDAO().retrieveByClause(DeptVO.class, "pk_dept='" + update_parentVO.getChargedept() + "'");
                    projectHeadVOs.setPk_duty_dept_v(dept.toArray(new DeptVO[0])[0].getPk_vid());
                }
                // 5.负责人
                if (!update_parentVO.getChargeman().equals(pjappl_parentVO.getHeaduser())) {
                    pjappl_parentVO.setHeaduser(update_parentVO.getChargeman());

                    projectHeadVOs.setPk_dutier(update_parentVO.getChargeman());
                }
                // 6.申请科室
                if (!update_parentVO.getApplydept().equals(pjappl_parentVO.getExeoffece())) {
                    pjappl_parentVO.setExeoffece(update_parentVO.getApplydept());
                }
                // 7.起始日期
                if (!update_parentVO.getStartingdate().toString().split(" ")[0]
                        .equals(pjappl_parentVO.getStartingdate().toString().split(" ")[0])) {
                    pjappl_parentVO.setStartingdate(update_parentVO.getStartingdate());

                    projectHeadVOs.setPlan_start_date(update_parentVO.getStartingdate());
                }
                // 8.截止日期
                if (!update_parentVO.getClosingdate().toString().split(" ")[0]
                        .equals(pjappl_parentVO.getClosingdate().toString().split(" ")[0])) {
                    pjappl_parentVO.setClosingdate(update_parentVO.getClosingdate());

                    projectHeadVOs.setPlan_finish_date(update_parentVO.getClosingdate());
                }
                // 9.项目周期
                if (new UFDouble(update_parentVO.getProcycle().intValue())
                        .compareTo(pjappl_parentVO.getProcycle()) != 0) {
                    pjappl_parentVO.setProcycle(new UFDouble(update_parentVO.getProcycle().intValue()));
                }

//                if (update_parentVO.getArgumentrating().compareTo(pjappl_parentVO.getArgumentrating()) != 0) {
                    pjappl_parentVO.setArgumentrating(update_parentVO.getArgumentrating());
//                }

//                if (!update_parentVO.getArgumentdate().toString().split(" ")[0]
//                        .equals(pjappl_parentVO.getArgumentdate().toString().split(" ")[0])) {
                    pjappl_parentVO.setArgumentdate(update_parentVO.getArgumentdate());
//                }

//                if (!update_parentVO.getArgumentexplain().equals(pjappl_parentVO.getArgumentexplain())) {
                    pjappl_parentVO.setArgumentexplain(update_parentVO.getArgumentexplain());
//                }

//                if ((update_parentVO.getPriority().compareTo(pjappl_parentVO.getPriority()) != 0)) {
                    pjappl_parentVO.setPriority(update_parentVO.getPriority());
//                }

//                if (!update_parentVO.getPjdesc().equals(pjappl_parentVO.getPjdesc())) {
                    pjappl_parentVO.setPjdesc(update_parentVO.getPjdesc());
//                }

//                if (!update_parentVO.getPjcontents().equals(pjappl_parentVO.getPjcontents())) {
                    pjappl_parentVO.setPjcontents(update_parentVO.getPjcontents());
                    if ( update_parentVO.getPjcontents() != null && update_parentVO.getPjcontents().length() > 100)
                        projectHeadVOs.setMemo(update_parentVO.getPjcontents().substring(0,100));
                    else
                        projectHeadVOs.setMemo(update_parentVO.getPjcontents());
//                }

//                if (!update_parentVO.getPlanfeasibility().equals(pjappl_parentVO.getPlanfeasibility())) {
                    pjappl_parentVO.setPlanfeasibility(update_parentVO.getPlanfeasibility());
//                }

//                if (!update_parentVO.getPlannecessity().equals(pjappl_parentVO.getPlannecessity())) {
                    pjappl_parentVO.setPlannecessity(update_parentVO.getPlannecessity());
//                }

//                if (!update_parentVO.getPjperforobj().equals(pjappl_parentVO.getPjperforobj())) {
                    pjappl_parentVO.setPjperforobj(update_parentVO.getPjperforobj());
//                }

//                if (!update_parentVO.getGovernmentpurchase().equals(pjappl_parentVO.getGovernmentpurchase())) {
                    pjappl_parentVO.setGovernmentpurchase(update_parentVO.getGovernmentpurchase());
                    projectHeadVOs.setGovpurch(update_parentVO.getGovernmentpurchase());
//                }

//                if (!update_parentVO.getAddedassetallocation().equals(pjappl_parentVO.getAddedassetallocation())) {
                    pjappl_parentVO.setAddedassetallocation(update_parentVO.getAddedassetallocation());
                    projectHeadVOs.setAssetallocation(update_parentVO.getAddedassetallocation());
//                }

//                if (!update_parentVO.getExpertgroupmember().equals(pjappl_parentVO.getExpertgroupmember())) {
                    pjappl_parentVO.setExpertgroupmember(update_parentVO.getExpertgroupmember());
//                }

                pjappl_parentVO.setBperformance(update_parentVO.getBperformance());
                projectHeadVOs.setBperformance(update_parentVO.getBperformance());

                // 项目调整 资金来源子表
                ISuperVO[] update_funsource = vos[0].getChildren(ProUpdateBVO.class);
                if (update_funsource != null && update_funsource.length > 0) {
                    // 项目申请 资金来源子表
                    ISuperVO[] pjappl_funsource = pjapplVOs[0].getChildren(PjapplBVO.class);
                    if (pjappl_funsource == null || pjappl_funsource.length == 0) {
                        // 需要添加的b集合
                        ArrayList<PjapplBVO> need_add_b = new ArrayList<PjapplBVO>();
                        // 新建子表数据
                        for (int i = 0; i < update_funsource.length; i++) {
                            ProUpdateBVO b = (ProUpdateBVO) update_funsource[i];
                            PjapplBVO pjapplBVO = new PjapplBVO();
                            pjapplBVO.setStatus(2);
                            pjapplBVO.setCapitalsource(b.getPk_funsource());
                            pjapplBVO.setRowno(b.getRowno());
                            pjapplBVO.setAmount(b.getUpdaftermoney());
                            pjapplBVO.setRemark(b.getRemark());
                            pjapplBVO.setIsassort(UFBoolean.FALSE);

                            need_add_b.add(pjapplBVO);
                        }
                        pjapplVOs[0].setChildrenVO(need_add_b.toArray(new PjapplBVO[]{}));
                    } else {
                        ArrayList<PjapplBVO> need_add_b = new ArrayList<PjapplBVO>();
                        // 维护一个申请单子表的pk - vo 结果集
                        HashMap<String, PjapplBVO> pjappl_b_map = new HashMap<String, PjapplBVO>();
                        for (int i = 0; i < pjappl_funsource.length; i++) {
                            PjapplBVO pjappl_b = (PjapplBVO) pjappl_funsource[i];
                            pjappl_b_map.put(pjappl_b.getPrimaryKey(), pjappl_b);
                        }
                        // 修改旧的 添加新的
                        for (int i = 0; i < update_funsource.length; i++) {
                            ProUpdateBVO b = (ProUpdateBVO) update_funsource[i];
                            // 根据原始行标记和原始行id 确认原始行是否存在
                            if (b.getIsorigin_b().booleanValue() && pjappl_b_map.get(b.getSrcrowid_b()) != null) {
                                // 存在 则修改
                                PjapplBVO pjapplBVO = pjappl_b_map.get(b.getSrcrowid_b());
                                pjapplBVO.setStatus(1);
                                pjapplBVO.setCapitalsource(b.getPk_funsource());
                                pjapplBVO.setAmount(b.getUpdaftermoney());
                                pjapplBVO.setRemark(b.getRemark());

                                need_add_b.add(pjapplBVO);

                            } else {
                                // 非原始行 和 原始行不存在 都新增
                                PjapplBVO pjapplBVO = new PjapplBVO();
                                pjapplBVO.setStatus(2);
                                pjapplBVO.setCapitalsource(b.getPk_funsource());
                                pjapplBVO.setRowno(b.getRowno());
                                pjapplBVO.setAmount(b.getUpdaftermoney());
                                pjapplBVO.setRemark(b.getRemark());
                                pjapplBVO.setIsassort(UFBoolean.FALSE);

                                need_add_b.add(pjapplBVO);
                            }
                        }
                        pjapplVOs[0].setChildrenVO(need_add_b.toArray(new PjapplBVO[]{}));
                    }
                } else {
                    pjapplVOs[0].setChildrenVO(new PjapplBVO[]{});
                }

                // 项目调整 绩效指标子表
                ISuperVO[] update_jx = vos[0].getChildren(ProUpdateCVO.class);
                if (update_jx != null && update_jx.length > 0) {
                    // 项目申请 绩效指标子表
                    ISuperVO[] pjappl_jx = pjapplVOs[0].getChildren(PjapplCVO.class);
                    if (pjappl_jx == null || pjappl_jx.length == 0) {
                        // 需要添加的c集合
                        ArrayList<PjapplCVO> need_add_c = new ArrayList<PjapplCVO>();
                        // 新建子表数据
                        for (int i = 0; i < update_jx.length; i++) {
                            ProUpdateCVO c = (ProUpdateCVO) update_jx[i];
                            PjapplCVO pjapplCVO = new PjapplCVO();
                            pjapplCVO.setStatus(2);
                            pjapplCVO.setPk_jxaimstypev1(c.getPk_jxaimstypev1());
                            pjapplCVO.setPk_jxaimstypev2(c.getPk_jxaimstypev2());
                            pjapplCVO.setPk_jxaims(c.getPk_jxaims());
                            pjapplCVO.setOppvalue(c.getOppvalue());
                            pjapplCVO.setRemark(c.getRemark());

                            need_add_c.add(pjapplCVO);
                        }
                        pjapplVOs[0].setChildrenVO(need_add_c.toArray(new PjapplCVO[]{}));
                    } else {
                        ArrayList<PjapplCVO> need_add_c = new ArrayList<PjapplCVO>();
                        // 维护一个申请单子表的pk - vo 结果集
                        HashMap<String, PjapplCVO> pjappl_c_map = new HashMap<String, PjapplCVO>();
                        for (int i = 0; i < pjappl_jx.length; i++) {
                            PjapplCVO pjappl_c = (PjapplCVO) pjappl_jx[i];
                            pjappl_c_map.put(pjappl_c.getPrimaryKey(), pjappl_c);
                        }
                        // 修改旧的 添加新的
                        for (int i = 0; i < update_jx.length; i++) {
                            ProUpdateCVO c = (ProUpdateCVO) update_jx[i];
                            // 根据原始行标记和原始行id 确实原始行是否存在
                            if (c.getIsorigin_c().booleanValue() && pjappl_c_map.get(c.getSrcrowid_c()) != null) {
                                // 存在 则修改
                                PjapplCVO pjapplCVO = pjappl_c_map.get(c.getSrcrowid_c());
                                pjapplCVO.setStatus(1);
                                pjapplCVO.setPk_jxaimstypev1(c.getPk_jxaimstypev1());
                                pjapplCVO.setPk_jxaimstypev2(c.getPk_jxaimstypev2());
                                pjapplCVO.setPk_jxaims(c.getPk_jxaims());
                                pjapplCVO.setOppvalue(c.getOppvalue());
                                pjapplCVO.setRemark(c.getRemark());

                                need_add_c.add(pjapplCVO);
                            } else {
                                // 非原始行 和 原始行不存在 则新增
                                PjapplCVO pjapplCVO = new PjapplCVO();
                                pjapplCVO.setStatus(2);
                                pjapplCVO.setPk_jxaimstypev1(c.getPk_jxaimstypev1());
                                pjapplCVO.setPk_jxaimstypev2(c.getPk_jxaimstypev2());
                                pjapplCVO.setPk_jxaims(c.getPk_jxaims());
                                pjapplCVO.setOppvalue(c.getOppvalue());
                                pjapplCVO.setRemark(c.getRemark());

                                need_add_c.add(pjapplCVO);
                            }
                        }
                        pjapplVOs[0].setChildrenVO(need_add_c.toArray(new PjapplCVO[]{}));
                    }
                } else {
                    pjapplVOs[0].setChildrenVO(new PjapplCVO[]{});
                }

                // 项目调整 采购信息子表
                ISuperVO[] update_cg = vos[0].getChildren(CGUpdateCVO.class);
                if (update_cg != null && update_cg.length > 0) {
                    // 项目申请 采购信息子表
                    ISuperVO[] pjappl_cg = pjapplVOs[0].getChildren(PjapplPVO.class);
                    if (pjappl_cg == null || pjappl_cg.length == 0) {
                        // 需要添加的c集合
                        ArrayList<PjapplPVO> need_add_p = new ArrayList<PjapplPVO>();
                        // 新建子表数据
                        for (int i = 0; i < update_cg.length; i++) {
                            CGUpdateCVO p = (CGUpdateCVO) update_cg[i];
                            PjapplPVO pjapplPVO = new PjapplPVO();
                            pjapplPVO.setStatus(2);
                            pjapplPVO.setVbdef1(p.getVbdef1());
                            pjapplPVO.setVbdef2(p.getVbdef2());
                            pjapplPVO.setVbdef3(p.getVbdef3());
                            pjapplPVO.setVbdef4(p.getVbdef4());
                            pjapplPVO.setVbdef5(p.getVbdef5());
                            pjapplPVO.setVbdef6(p.getVbdef6());
                            pjapplPVO.setVbdef7(p.getVbdef7());
                            pjapplPVO.setVbdef8(p.getVbdef8());
                            pjapplPVO.setVbdef9(p.getVbdef9());
                            pjapplPVO.setVbdef10(p.getVbdef10());
                            pjapplPVO.setApplyno(p.getApplyno());
                            pjapplPVO.setName(p.getName());
                            pjapplPVO.setSpec(p.getSpec());
                            pjapplPVO.setKs(p.getKs());
                            pjapplPVO.setPrice(p.getPrice());
                            pjapplPVO.setNnum(p.getNnum());
                            pjapplPVO.setNorigtaxmny(p.getNorigtaxmny());
                            pjapplPVO.setRemark(p.getRemark());
                            need_add_p.add(pjapplPVO);
                        }
                        pjapplVOs[0].setChildrenVO(need_add_p.toArray(new PjapplPVO[]{}));
                    } else {
                        ArrayList<PjapplPVO> need_add_p = new ArrayList<PjapplPVO>();
                        Set<String> listpks = new HashSet<>();
                        // 维护一个申请单子表的pk - vo 结果集
                        HashMap<String, PjapplPVO> pjappl_c_map = new HashMap<String, PjapplPVO>();
                        for (int i = 0; i < pjappl_cg.length; i++) {
                            PjapplPVO pjappl_c = (PjapplPVO) pjappl_cg[i];
                            String key = pjappl_c.getVbdef1();// +pjappl_c.getVbdef2();
                            pjappl_c_map.put(key, pjappl_c);
                        }
                        // 修改旧的 添加新的
                        for (int i = 0; i < update_cg.length; i++) {
                            CGUpdateCVO p = (CGUpdateCVO) update_cg[i];
                            // 根据原始行标记和原始行id 确实原始行是否存在
                            Object isorigin_c = p.getAttributeValue("isorigin_c");
                            if ("Y".equals(isorigin_c+"")) {
                                // 存在 则修改
                                Object vbdef1 = p.getAttributeValue("vbdef1");
                                Object vbdef2 = p.getAttributeValue("vbdef2");
                                String key = vbdef1.toString();// +vbdef2;
                                PjapplPVO pjapplPVO = pjappl_c_map.get(key);
                                pjapplPVO.setStatus(VOStatus.UPDATED);
                                pjapplPVO.setAttributeValue("applyno",p.getAttributeValue("applyno"));
                                pjapplPVO.setAttributeValue("name",p.getAttributeValue("name"));
                                pjapplPVO.setAttributeValue("spec",p.getAttributeValue("spec"));
                                pjapplPVO.setAttributeValue("ks",p.getAttributeValue("ks"));
                                pjapplPVO.setAttributeValue("price",p.getAttributeValue("price"));
                                pjapplPVO.setAttributeValue("nnumm",p.getAttributeValue("nnum"));
                                pjapplPVO.setAttributeValue("norigtaxmny",p.getAttributeValue("norigtaxmny"));
                                pjapplPVO.setAttributeValue("remark",p.getAttributeValue("remark"));

                                pjapplPVO.setAttributeValue("vbdef1",p.getAttributeValue("vbdef1"));
                                pjapplPVO.setAttributeValue("vbdef2",p.getAttributeValue("vbdef2"));
                                pjapplPVO.setAttributeValue("vbdef3",p.getAttributeValue("vbdef3"));
                                pjapplPVO.setAttributeValue("vbdef4",p.getAttributeValue("vbdef4"));
                                pjapplPVO.setAttributeValue("vbdef5",p.getAttributeValue("vbdef5"));
                                pjapplPVO.setAttributeValue("vbdef6",p.getAttributeValue("vbdef6"));
                                pjapplPVO.setAttributeValue("vbdef7",p.getAttributeValue("vbdef7"));
                                pjapplPVO.setAttributeValue("vbdef8",p.getAttributeValue("vbdef8"));
                                pjapplPVO.setAttributeValue("vbdef9",p.getAttributeValue("vbdef9"));
                                pjapplPVO.setAttributeValue("vbdef10",p.getAttributeValue("vbdef10"));

                                need_add_p.add(pjapplPVO);
                            } else {
                                // 非原始行 和 原始行不存在 则新增
                                PjapplPVO pjapplPVO = new PjapplPVO();
                                pjapplPVO.setStatus(2);
                                pjapplPVO.setAttributeValue("vbdef3", new UFDate());
                                pjapplPVO.setAttributeValue("vbdef1",p.getAttributeValue("vbdef1"));
                                pjapplPVO.setAttributeValue("vbdef2",p.getAttributeValue("vbdef2"));
                                pjapplPVO.setAttributeValue("vbdef4",p.getAttributeValue("vbdef4"));
                                pjapplPVO.setAttributeValue("vbdef5",p.getAttributeValue("vbdef5"));
                                pjapplPVO.setAttributeValue("vbdef6",p.getAttributeValue("vbdef6"));
                                pjapplPVO.setAttributeValue("vbdef7",p.getAttributeValue("vbdef7"));
                                pjapplPVO.setAttributeValue("vbdef8",p.getAttributeValue("vbdef8"));
                                pjapplPVO.setAttributeValue("vbdef9",p.getAttributeValue("vbdef9"));
                                pjapplPVO.setAttributeValue("vbdef10",p.getAttributeValue("vbdef10"));
                                pjapplPVO.setAttributeValue("applyno",p.getAttributeValue("applyno"));
                                pjapplPVO.setAttributeValue("name",p.getAttributeValue("name"));
                                pjapplPVO.setAttributeValue("spec",p.getAttributeValue("spec"));
                                pjapplPVO.setAttributeValue("ks",p.getAttributeValue("ks"));
                                pjapplPVO.setAttributeValue("price",p.getAttributeValue("price"));
                                pjapplPVO.setAttributeValue("nnumm",p.getAttributeValue("nnum"));
                                pjapplPVO.setAttributeValue("norigtaxmny",p.getAttributeValue("norigtaxmny"));
                                pjapplPVO.setAttributeValue("remark",p.getAttributeValue("remark"));
                                need_add_p.add(pjapplPVO);
                            }
                        }
                        pjapplVOs[0].setChildrenVO(need_add_p.toArray(new PjapplPVO[]{}));
                    }
                } else {
                    pjapplVOs[0].setChildrenVO(new PjapplPVO[]{});
                }

                // 更新申请单
                IPJApplHVOService ipjApplHVOService = NCLocator.getInstance().lookup(IPJApplHVOService.class);
                AggPjapplVO[] saveAggPjapplVO = ipjApplHVOService.saveAggPjapplVO(pjapplVOs[0]);

                // 更新项目-集团/项目-组织 对应的项目档案
                IProject iProject = NCLocator.getInstance().lookup(IProject.class);
                ProjectBillVO[] updateProject = iProject.update(projectBillVOs);

                //***************************************处理科研:立项任务书***********************************************
//                boolean installedAndEnabledHrpom = isInstalledAndEnabledHrpom();
//                if (installedAndEnabledHrpom) {
//                    //获取对应的立项任务书主键
//                    String[] pk_prosetuptasks = (String[]) getBaseDAO().executeQuery(" select pk_prosetuptask from hrpom_prosetuptask where dr = 0 and islastversion = 'Y' and pk_project = '" + vos[0].getParentVO().getPk_proapp() + "'", (rs) -> {
//                        List<String> pks = new ArrayList<>();
//                        while (rs.next()) {
//                            pks.add(rs.getString(1));
//                        }
//                        return pks.toArray(new String[0]);
//                    });
//                    //待更新的立项任务书
//                    AggProSetupTaskVO[] aggProSetupTaskVOS = null;
//                    AggProSetupTaskVO[] originVos = null; //复制一份原始单据 调用更新方法需要
//                    IProsetuptaskvoMaintain prosetuptaskvoMaintain = NCLocator.getInstance().lookup(IProsetuptaskvoMaintain.class);
//                    if (ArrayUtils.isNotEmpty(pk_prosetuptasks)) {
//                        aggProSetupTaskVOS = prosetuptaskvoMaintain.queryByPKs(pk_prosetuptasks);
//                        originVos = new AggProSetupTaskVO[aggProSetupTaskVOS.length];
//                    }
//
//                    //当前版本立项任务书只更新表头信息字段 不动经费
//                    //可能不存在
//                    //需要修改经费 否则调整单调整经费的时候 立项任务书保存校验会报错
//                    if (ArrayUtils.isNotEmpty(aggProSetupTaskVOS)) {
//                        for (int i = 0; i < aggProSetupTaskVOS.length; i++) {
//                            //保存一份原始单据
//                            AggProSetupTaskVO clone = (AggProSetupTaskVO) aggProSetupTaskVOS[i].clone();
//                            originVos[i] = clone;
//
//                            aggProSetupTaskVOS[i].getParentVO().setStatus(1); //修改状态
//                            aggProSetupTaskVOS[i].getParentVO().setName(update_parentVO.getProname()); //项目名称
//                            aggProSetupTaskVOS[i].getParentVO().setStartdate(update_parentVO.getStartingdate()); //开始时间
//                            aggProSetupTaskVOS[i].getParentVO().setEnddate(update_parentVO.getClosingdate()); //结束时间
//                            aggProSetupTaskVOS[i].getParentVO().setPk_dept_main(update_parentVO.getChargedept()); //主管科室
//                            aggProSetupTaskVOS[i].getParentVO().setPk_director(update_parentVO.getChargeman()); //项目负责人
//                            aggProSetupTaskVOS[i].getParentVO().setPk_dept_accept(update_parentVO.getApplydept()); //承担科室
//
//                            //更新课题经费页签
//                            if (update_funsource != null && update_funsource.length > 0) {
//                                ISuperVO[] children = aggProSetupTaskVOS[i].getChildren(TopicOutlayVO.class);
//                                if (children != null && children.length > 0) {
//                                    //资金来源->topicOutlayVo
//                                    HashMap<String, TopicOutlayVO> funSource_topicOutlayVo = new HashMap<>();
//                                    for (ISuperVO child : children) {
//                                        TopicOutlayVO t = (TopicOutlayVO) child;
//                                        funSource_topicOutlayVo.put(t.getPk_fundsrc(),t);
//                                    }
//                                    //资金来源->ProUpdateBVO
//                                    HashMap<String, ProUpdateBVO> funSource_proUpdateBVo = new HashMap<>();
//
//                                    ArrayList<TopicOutlayVO> new_topicOutlays = new ArrayList<>();
//
//                                    for (ISuperVO iSuperVO : update_funsource) {
//                                        ProUpdateBVO b = (ProUpdateBVO) iSuperVO;
//                                        funSource_proUpdateBVo.put(b.getPk_funsource(),b);
//                                        if (funSource_topicOutlayVo.containsKey(b.getPk_funsource())){
//                                            TopicOutlayVO topicOutlayVO = funSource_topicOutlayVo.get(b.getPk_funsource());
//                                            topicOutlayVO.setStatus(1);
//                                            topicOutlayVO.setMoney(b.getUpdaftermoney());
//                                            new_topicOutlays.add(topicOutlayVO);
//                                        }else{
//                                            TopicOutlayVO topicOutlayVO = new TopicOutlayVO();
//                                            topicOutlayVO.setStatus(2);
//                                            topicOutlayVO.setPk_fundsrc(b.getPk_funsource());
//                                            topicOutlayVO.setMoney(b.getUpdaftermoney());
//                                            new_topicOutlays.add(topicOutlayVO);
//                                        }
//                                    }
//                                    //再添加上立项任务书有的 项目调整没有的
//                                    for (ISuperVO child : children) {
//                                        TopicOutlayVO t = (TopicOutlayVO) child;
//                                        if (!funSource_proUpdateBVo.containsKey(t.getPk_fundsrc())){
//                                            t.setStatus(0);
//                                            new_topicOutlays.add(t);
//                                        }
//                                    }
//                                    UFDouble total_money = UFDouble.ZERO_DBL;
//                                    for (TopicOutlayVO newTopicOutlay : new_topicOutlays) {
//                                        total_money = total_money.add(newTopicOutlay.getMoney());
//                                    }
//                                    //重新统计课题总经费
//                                    aggProSetupTaskVOS[i].getParentVO().setTopicmoney(total_money); //课题总经费
//                                    aggProSetupTaskVOS[i].setChildren(TopicOutlayVO.class,new_topicOutlays.toArray(new TopicOutlayVO[0]));
//                                }
//                            }
//
//                            //更新参加单位页签
//                            ISuperVO[] children = aggProSetupTaskVOS[i].getChildren(JoinUnitVO.class);
//                            if (children != null && children.length > 0){
//                                //本单位总经费
//                                UFDouble unitmoney = UFDouble.ZERO_DBL;
//                                //应拨协作单位经费
//                                UFDouble coopunitmoney = UFDouble.ZERO_DBL;
//                                for (ISuperVO child : children) {
//                                    JoinUnitVO c = (JoinUnitVO) child;
////                                    String pkCust = c.getPk_cust();
////                                    if (pkCust.equals(update_parentVO.getPk_org())){
//                                    if (c.getUnitrole() == 1){ //牵头单位
//                                        c.setEarmarkmoney(update_parentVO.getUpdallfun().sub(c.getEarmarkmoney()).add(c.getEarmarkmoney()));
//                                        c.setSummoney(c.getEarmarkmoney().add(c.getCounterfund()));
//                                        c.setStatus(1);
//                                    }
//                                    if (c.getUnitrole() == 1){
//                                        unitmoney = unitmoney.add(c.getSummoney());
//                                    } else if (c.getUnitrole() == 2) {
//                                        coopunitmoney = coopunitmoney.add(c.getSummoney());
//                                    }
//                                }
//                                aggProSetupTaskVOS[i].getParentVO().setUnitmoney(unitmoney);
//                                aggProSetupTaskVOS[i].getParentVO().setCoopunitmoney(coopunitmoney);
//                            }
//                        }
//
//                    }
//                    //更新对应的立项任务书
//                    if (ArrayUtils.isNotEmpty(aggProSetupTaskVOS)) {
//                        AggProSetupTaskVO[] update = prosetuptaskvoMaintain.update(aggProSetupTaskVOS, originVos);
//                    }
//                }
            }

            return workFlowRet == null ? vos : workFlowRet;
        } catch (Exception ex) {
            if (ex instanceof BusinessException) {
                throw (BusinessException) ex;
            }
            throw new PFBusinessException(ex.getMessage(), ex);
        }
    }

    protected void before(PfParameterVO paraVO) throws BusinessException {
        AggProUpdateHVO vo = (AggProUpdateHVO) this.getVo();
        if (StringUtils.isEmpty(vo.getPrimaryKey())) {
            throw new BusinessException("当前单据未执行保存动作脚本，如果配置了业务流程定义，请检查动作驱动配置！");
        }
    }
}